package com.sdkj.service.base.quartz;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import com.sdkj.base.quartz.QuartzJobFactory;
import com.sdkj.entity.ScheduleJob;

/**
 * 
 * @Description: 计划任务管理
 * @author leiweiping
 * @date 2017年6月6日 下午20:43:54
 */
@Service
public class JobTaskService {
	public final Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	
	@Autowired
	private ScheduleJobService scheduleJobService;

	/**
	 * 添加任务
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void addJob(ScheduleJob job) throws SchedulerException {
		if (job == null) {
			return;
		}

		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		log.debug(scheduler + "add");

		JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class).withIdentity(job.getJobName(), job.getJobGroup()).build();
		jobDetail.getJobDataMap().put("scheduleJob", job);
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getExpression());
		CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
		scheduler.scheduleJob(jobDetail, trigger);
		
		try {
			scheduleJobService.save(job);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@PostConstruct
	public void init() throws Exception {
		// 这里获取所有任务信息数据
		List<ScheduleJob> jobList = scheduleJobService.listAll();
	
		for (ScheduleJob job : jobList) {
			addJob(job);
		}
	}

	/**
	 * 暂停一个job
	 * @param Job名称
	 * @param Job组名
	 * @throws SchedulerException
	 */
	public void pauseJob(String jobName,String jobGroup) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
		scheduler.pauseJob(jobKey);
	}

	/**
	 * 恢复一个job
	 * @param Job名称
	 * @param Job组名
	 * @throws SchedulerException
	 */
	public void resumeJob(String jobName,String jobGroup) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 删除一个job
	 * @param Job名称
	 * @param Job组名
	 * @throws SchedulerException
	 */
	public void deleteJob(String jobName,String jobGroup) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
		scheduler.deleteJob(jobKey);

	}

	/**
	 * 立即执行job
	 * @param Job名称
	 * @param Job组名
	 * @throws SchedulerException
	 */
	public void runAJobNow(String jobName,String jobGroup) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
		scheduler.triggerJob(jobKey);
	}

	/**
	 * 更新job时间表达式
	 * @param Job名称
	 * @param Job组名
	 * @param job时间表达式
	 * @throws SchedulerException
	 */
	public void updateJobCron(String jobName,String jobGroup,String expression) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(expression);
		trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
		scheduler.rescheduleJob(triggerKey, trigger);
	}
}
